﻿

Public Class PanelCompression

    'Load :
    Private Sub PanelCompression_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        KCB_Encoding.Items.Clear()
        For Each enci As System.Text.EncodingInfo In System.Text.Encoding.GetEncodings
            KCB_Encoding.Items.Add(enci.Name)
        Next
    End Sub
    'Fields :
    Private _LocalCompression As New Compression
    Private _TargetNode As ZipNode
    Public WriteOnly Property TargetNode As ZipNode
        Set(value As ZipNode)
            _TargetNode = value
            _LocalCompression.CopyFrom(_TargetNode.ZipSettings.Compression)
            ShowCompression()
        End Set
    End Property
    'Showing :
    Private Sub ShowCompression()
        Me.SuspendLayout()
        KCB_Method.SelectedIndex = _LocalCompression.Method.ToIndex
        KCB_Level.SelectedIndex = CInt(_LocalCompression.Level)
        KCB_Strategy.SelectedIndex = CInt(_LocalCompression.Strategy)
        KCB_Encoding.SelectedItem = _LocalCompression.Encoding.BodyName
        KCB_EncodingUsage.SelectedIndex = CInt(_LocalCompression.EncodingUsage)
        KCB_Zip64Usage.SelectedIndex = CInt(_LocalCompression.Zip64Usage)
        Me.ResumeLayout()
    End Sub
    'Resets :
    Private Sub BSA_ResetMethod_Click(sender As Object, e As System.EventArgs) Handles BSA_ResetMethod.Click
        KCB_Method.SelectedIndex = Compression.Defaults.Method.ToIndex
    End Sub
    Private Sub BSA_ResetLevel_Click(sender As Object, e As System.EventArgs) Handles BSA_ResetLevel.Click
        KCB_Level.SelectedIndex = CInt(Compression.Defaults.Level)
    End Sub
    Private Sub BSA_ResetStrategy_Click(sender As Object, e As System.EventArgs) Handles BSA_ResetStrategy.Click
        KCB_Strategy.SelectedIndex = CInt(Compression.Defaults.Strategy)
    End Sub
    Private Sub BSA_ResetEncoding_Click(sender As Object, e As System.EventArgs) Handles BSA_ResetEncoding.Click
        KCB_Encoding.SelectedItem = Compression.Defaults.Encoding.BodyName
    End Sub
    Private Sub BSA_ResetEncodingUsage_Click(sender As Object, e As System.EventArgs) Handles BSA_ResetEncodingUsage.Click
        KCB_EncodingUsage.SelectedIndex = CInt(Compression.Defaults.EncodingUsage)
    End Sub
    Private Sub BSA_ResetZip64Usage_Click(sender As Object, e As System.EventArgs) Handles BSA_ResetZip64Usage.Click
        KCB_Zip64Usage.SelectedIndex = CInt(Compression.Defaults.Zip64Usage)
    End Sub
    'Selections :
    Private Sub KCB_Method_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles KCB_Method.SelectedIndexChanged
        Select Case KCB_Method.SelectedIndex
            Case 0
                KCB_Level.SelectedIndex = 0
                KCB_Level.Enabled = False
                KCB_Strategy.Enabled = False
            Case 1
                KCB_Level.SelectedIndex = 6
                KCB_Level.Enabled = True
                KCB_Strategy.Enabled = True
            Case 2
                KCB_Level.SelectedIndex = 0
                KCB_Level.Enabled = False
                KCB_Strategy.Enabled = True
        End Select
    End Sub
    Private Sub KCB_Level_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles KCB_Level.SelectedIndexChanged
        If KCB_Level.SelectedIndex = 0 Then
            KCB_Method.SelectedIndex = 0
        End If
    End Sub
    'Action :
    Public Sub Reset()
        _LocalCompression.Reset()
        ShowCompression()
    End Sub
    Public Sub Cancel()
        _LocalCompression.CopyFrom(_TargetNode.ZipSettings.Compression)
        ShowCompression()
    End Sub
    Public Sub Apply()
        Select Case KCB_Method.SelectedIndex
            Case 0
                _TargetNode.ZipSettings.Compression.Method = Zip.CompressionMethod.None
            Case 1
                _TargetNode.ZipSettings.Compression.Method = Zip.CompressionMethod.Deflate
            Case 2
                _TargetNode.ZipSettings.Compression.Method = Zip.CompressionMethod.BZip2
        End Select
        _TargetNode.ZipSettings.Compression.Level = CType(KCB_Level.SelectedIndex, Zlib.CompressionLevel)
        _TargetNode.ZipSettings.Compression.Strategy = CType(KCB_Strategy.SelectedIndex, Zlib.CompressionStrategy)
        _TargetNode.ZipSettings.Compression.Encoding = System.Text.Encoding.GetEncoding(KCB_Encoding.SelectedItem.ToString)
        _TargetNode.ZipSettings.Compression.EncodingUsage = CType(KCB_EncodingUsage.SelectedIndex, Zip.ZipOption)
        _TargetNode.ZipSettings.Compression.Zip64Usage = CType(KCB_Zip64Usage.SelectedIndex, Zip.Zip64Option)
        Me.TargetNode = _TargetNode
    End Sub

End Class
